import { NavBar, DatePicker } from "antd-mobile";
import "./index.scss";
import { useMemo, useState, useEffect } from "react";
import classNames from "classnames";
import dayjs from "dayjs";
import { useSelector } from "react-redux";
import _ from "lodash";
import DayBill from "./components/DayBill";

const Month = () => {
  // 按月数据分组
  const billList = useSelector((state) => state.bill.billList);
  const monthGroup = useMemo(() => {
    // return出去计算后的值
    return _.groupBy(billList, (item) => dayjs(item.date).format("YYYY-MM"));
  }, [billList]);
  console.log(monthGroup, "monthGroup");

  // datePicker
  const [dateVisiable, setDateVisiable] = useState(false);

  // 控制日期显示
  const [currentDate, setCurrentDate] = useState(() =>
    dayjs(new Date()).format("YYYY-MM")
  );

  const [currentMonthData, setCurrentMonthData] = useState([]);

  const monthResult = useMemo(() => {
    const pay = currentMonthData
      .filter((item) => item.type === "pay")
      .reduce((a, c) => a + c.money, 0);
    const receive = currentMonthData
      .filter((item) => item.type === "receive")
      .reduce((a, c) => a + c.money, 0);
    return {
      pay,
      receive,
      total: pay + receive,
    };
  }, [currentMonthData]);

  useEffect(() => {
    const nowDate = dayjs(new Date()).format("YYYY-MM");
    if (monthGroup[nowDate]) {
      setCurrentMonthData(monthGroup[nowDate]);
    }
  }, [monthGroup]);

  // 确认回调
  const onConfirm = (date) => {
    setDateVisiable(false);
    const formatDate = dayjs(date).format("YYYY-MM");
    setCurrentDate(formatDate);
    setCurrentMonthData(monthGroup[formatDate]);
  };

  // 当前月按照日来分组
  const dayGroup = useMemo(() => {
    const groupData = _.groupBy(currentMonthData, (item) =>
      dayjs(item.date).format("YYYY-MM-DD")
    );
    const keys = Object.keys(groupData);
    // return出去计算后的值
    return {
      groupData,
      keys,
    };
  }, [currentMonthData]);

  return (
    <div className="monthlyBill">
      <NavBar className="nav" backArrow={false}>
        月度收支
      </NavBar>
      <div className="content">
        <div className="header">
          {/* 时间切换区域 */}
          <div className="date" onClick={() => setDateVisiable(true)}>
            <span className="text">
              {currentDate}
              月账单
            </span>
            <span
              className={classNames("arrow", dateVisiable && "expand")}
            ></span>
          </div>
          {/* 统计区域 */}
          <div className="twoLineOverview">
            <div className="item">
              <span className="money">{monthResult.pay.toFixed(2)}</span>
              <span className="type">支出</span>
            </div>
            <div className="item">
              <span className="money">{monthResult.receive.toFixed(2)}</span>
              <span className="type">收入</span>
            </div>
            <div className="item">
              <span className="money">{monthResult.total.toFixed(2)}</span>
              <span className="type">结余</span>
            </div>
          </div>
          {/* 时间选择器 */}
          <DatePicker
            className="kaDate"
            title="记账日期"
            precision="month"
            visible={dateVisiable}
            onCancel={() => setDateVisiable(false)}
            onConfirm={onConfirm}
            onClose={() => setDateVisiable(false)}
            max={new Date()}
          />
        </div>

        {dayGroup.keys.map((key) => {
          return (
            <DayBill date={key} billList={dayGroup.groupData[key]} key={key} />
          );
        })}
      </div>
    </div>
  );
};

export default Month;
